/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.luyu.config;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import jakarta.annotation.Resource;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 创建Swagger配置
 * @since:knife4j-springdoc-openapi-demo 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2020/03/15 20:40
 */
@Configuration
@EnableConfigurationProperties(Knife4jProperty.class)
public class SwaggerConfig {
    @Resource
    private Knife4jProperty knife4jProperty;

    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            // if (openApi.getTags() != null) {
            //     openApi.getTags().forEach(tag -> {
            //         Map<String, Object> map = new HashMap<>();
            //         map.put("x-order", RandomUtil.randomInt(0, 100));
            //         tag.setExtensions(map);
            //     });
            // }
            // if (openApi.getPaths() != null) {
            //     openApi.addExtension("x-test123", "333");
            //     openApi.getPaths().addExtension("x-abb", RandomUtil.randomInt(1, 100));
            // }
        };
    }

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(knife4jProperty.getTitle())
                        .version(knife4jProperty.getVersion())
                        .description(knife4jProperty.getDescription())
                        .termsOfService(knife4jProperty.getTermService())
                        .license(new License().name(knife4jProperty.getLicenseName())
                                .url(knife4jProperty.getLicenseUrl()
                                )
                        )
                );
    }


}
